<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="60%"
    class="component-user-label-edit el-dialog-box"
    @close="onCloseClr()"
  >
    <div class="edit-label-content">
      <div class="label-name-line currency-line">
        <div class="title">
          <span class="must-term">*</span>{{ $t('user.labelName') }}
        </div>
        <div>
          <el-input
            v-model="labelName"
            type="text"
            maxlength="10"
            show-word-limit
            @blur="labelName = onHandleInputSpaces(labelName)"
          />
        </div>
      </div>
      <div class="label-type-line currency-line">
        <div class="title">
          <span class="must-term">*</span>{{ $t('user.labelType') }}
        </div>
        <el-radio
          v-model="labelType"
          label="1"
          :disabled="isDisaCondiLabel"
        >
          {{ $t('user.conditionLabel') }}
        </el-radio>
        <el-radio
          v-model="labelType"
          label="0"
          :disabled="isDisaCustLabel"
        >
          {{ $t('user.manualLabel') }}
        </el-radio>
      </div>
      <div v-if="labelType==1">
        <div class="label-condition-title-line">
          {{ $t('user.conditionSetting') }}<span class="tips">{{ $t('user.conditionSettingTips') }}</span>
        </div>
        <div class="basic-condition-line currency-line">
          <div class="title">
            {{ $t('user.basicCondition') }}
          </div>
          <div class="basic-set-content module-content">
            <div class="option-line">
              <el-checkbox v-model="isBeCustTimeChecked">
                {{ $t('user.becomeCustomerTime') }}
              </el-checkbox>
              <el-select
                v-model="beCustTimeValue"
                size="large"
                :disabled="!isBeCustTimeChecked"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in beCustTimeOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <div
                v-if="isBeCustTimeChecked"
                class="block"
              >
                <el-date-picker
                  v-model="beCustTimeRange"
                  type="daterange"
                  size="large"
                  :default-time="[new Date(2000, 1, 1, 0, 0, 0),
                                  new Date(2000, 2, 1, 23, 59, 59)]"
                  range-separator="-"
                  :start-placeholder="$t('user.startDate')"
                  :end-placeholder="$t('user.endDate')"
                  @change="onChagCustTimeRange(0)"
                />
              </div>
            </div>
            <div class="option-line">
              <el-checkbox v-model="isBeMemTimeChecked">
                {{ $t('user.becomeMemberTime') }}
              </el-checkbox>
              <el-select
                v-model="beMemTimeValue"
                size="large"
                :disabled="!isBeMemTimeChecked"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in beMemTimeOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <div
                v-if="isBeMemTimeChecked"
                class="block"
              >
                <el-date-picker
                  v-model="beMemTimeRange"
                  type="daterange"
                  size="large"
                  :default-time="[new Date(2000, 1, 1, 0, 0, 0),
                                  new Date(2000, 2, 1, 23, 59, 59)]"
                  range-separator="-"
                  :start-placeholder="$t('user.startDate')"
                  :end-placeholder="$t('user.endDate')"
                  @change="onChagCustTimeRange(2)"
                />
              </div>
            </div>
          </div>
        </div>
        <div class="transaction-condition-line currency-line">
          <div class="title">
            {{ $t('user.tradingConditions') }}
          </div>
          <div class="deal-condition-content module-content">
            <div class="option-line">
              <el-checkbox v-model="isNearConTime">
                {{ $t('user.nearConsuTime') }}
              </el-checkbox>
              <el-select
                v-model="nearConTimeValue"
                size="large"
                :disabled="!isNearConTime"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in nearConTimeOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </div>
            <div class="option-line">
              <el-checkbox v-model="isConNum">
                {{ $t('user.consuNum') }}
              </el-checkbox>
              <el-select
                v-model="conNumValue"
                size="large"
                :disabled="!isConNum"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in conNumOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <div class="unit-wrapper first-iterm">
                <el-input
                  v-model="conNumBegInp"
                  type="number"
                  min="0"
                  size="large"
                  :disabled="!isConNum"
                  class="cur-inp"
                  @blur="onCheckNumber (1)"
                />
                <span class="unit">{{ $t('user.bout') }}</span>
              </div>
              <span class="interval-line">-</span>
              <div class="unit-wrapper">
                <el-input
                  v-model="conNumEndInp"
                  type="number"
                  min="0"
                  size="large"
                  :disabled="!isConNum"
                  class="cur-inp"
                  @blur="onCheckNumber (2)"
                />
                <span class="unit">{{ $t('user.bout') }}</span>
              </div>
            </div>
            <div class="option-line">
              <el-checkbox v-model="isConAmount">
                {{ $t('user.consuAmount') }}
              </el-checkbox>
              <el-select
                v-model="conAmountValue"
                size="large"
                :disabled="!isConAmount"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in conAmountOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <div class="unit-wrapper first-iterm">
                <el-input
                  v-model="conAmountBegInp"
                  type="number"
                  min="0"
                  size="large"
                  :disabled="!isConAmount"
                  class="cur-inp"
                  @blur="onCheckNumber (3)"
                />
                <span class="unit">{{ $t('user.yuan') }}</span>
              </div>
              <span class="interval-line">-</span>
              <div class="unit-wrapper">
                <el-input
                  v-model="conAmountEndInp"
                  type="number"
                  min="0"
                  size="large"
                  :disabled="!isConAmount"
                  class="cur-inp"
                  @blur="onCheckNumber (4)"
                />
                <span class="unit">{{ $t('user.yuan') }}</span>
              </div>
            </div>
            <div class="option-line">
              <el-checkbox v-model="isOrderAverPrice">
                {{ $t('user.averPri') }}
              </el-checkbox>
              <el-select
                v-model="orderAverPriceValue"
                size="large"
                :disabled="!isOrderAverPrice"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in orderAverPriceOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <div class="unit-wrapper first-iterm">
                <el-input
                  v-model="orderAverPriceBegInp"
                  type="number"
                  min="0"
                  size="large"
                  :disabled="!isOrderAverPrice"
                  class="cur-inp"
                  @blur="onCheckNumber (5)"
                />
                <span class="unit">{{ $t('user.yuan') }}</span>
              </div>
              <span class="interval-line">-</span>
              <div class="unit-wrapper">
                <el-input
                  v-model="orderAverPriceEndInp"
                  type="number"
                  min="0"
                  size="large"
                  :disabled="!isOrderAverPrice"
                  class="cur-inp"
                  @blur="onCheckNumber (6)"
                />
                <span class="unit">{{ $t('user.yuan') }}</span>
              </div>
            </div>
          </div>
        </div>
        <div class="transaction-condition-line currency-line">
          <div class="title">
            {{ $t('user.rechargeConditions') }}
          </div>
          <div class="deal-condition-content module-content">
            <div class="option-line">
              <el-checkbox v-model="isRecharNum">
                {{ $t('user.rechargeNumber') }}
              </el-checkbox>
              <el-select
                v-model="recharNumValue"
                size="large"
                :disabled="!isRecharNum"
                :placeholder="$t('user.pleaseSelect')"
              >
                <el-option
                  v-for="item in recharNumOpts"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <div class="unit-wrapper first-iterm">
                <el-input
                  v-model="recharNumBegInp"
                  type="number"
                  min="0"
                  size="large"
                  :disabled="!isRecharNum"
                  class="cur-inp"
                  @blur="onCheckNumber (9)"
                />
                <span class="unit">{{ $t('user.bout') }}</span>
              </div>
              <span class="interval-line">-</span>
              <div class="unit-wrapper">
                <el-input
                  v-model="recharNumEndInp"
                  type="number"
                  min="0"
                  size="large"
                  :disabled="!isRecharNum"
                  class="cur-inp"
                  @blur="onCheckNumber (10)"
                />
                <span class="unit">{{ $t('user.bout') }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 按键 -->
    <template #footer>
      <span

        class="dialog-footer"
      >
        <div
          class="default-btn"
          @click="dialogVisible = false"
        >{{ $t('user.calcel') }}</div>
        <div
          class="default-btn primary-btn"
          @click="onPreserveLabel"
        >{{ $t('user.preservation') }}</div>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import moment from 'moment'
import { watch } from 'vue'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['refreshLabelList', 'refreshLabelList', 'refreshLabelList'])

const title = ref($t('user.addLabel'))
const dialogVisible = ref(false)
const isDisaCondiLabel = ref(false)
const isDisaCustLabel = ref(false)
const labelName = ref('')
const labelType = ref('1')
// 基础条件
const basicConOpts = [{
  value: 0,
  label: $t('user.today')
}, {
  value: 1,
  label: $t('user.yesterday')
}, {
  value: 2,
  label: $t('user.lastSevenDay')
}, {
  value: 3,
  label: $t('user.lastThirtyDay')
}, {
  value: 4,
  label: $t('user.thisMonth')
}, {
  value: 5,
  label: $t('user.lastMonth')
}, {
  value: 6,
  label: $t('user.customRange')
}]
// 成为客户时间
const isBeCustTimeChecked = ref(false)
const beCustTimeValue = ref(0)

const beCustTimeOpts = ref([])
const beCustTimeRange = ref('')
// // 关注时间
// isfollowTimeChecked: false
let isfollowTimeChecked = false
const followTimeValue = ref(0)
// followTimeRange: ''
let followTimeRange = ''
// 成为会员时间
const isBeMemTimeChecked = ref(false)
const beMemTimeValue = ref(0)
const beMemTimeOpts = ref([])
const beMemTimeRange = ref('')
// 交易条件
const dealConditionOpts = [{
  value: 1,
  label: $t('user.today')
}, {
  value: 2,
  label: $t('user.lastSevenDay')
}, {
  value: 3,
  label: $t('user.lastFifteenDay')
}, {
  value: 4,
  label: $t('user.lastThirtyDay')
}, {
  value: 5,
  label: $t('user.lastFortyFiveDay')
}, {
  value: 6,
  label: $t('user.lastSixtyDay')
}, {
  value: 7,
  label: $t('user.lastNinetyDay')
}, {
  value: 8,
  label: $t('user.lastOneHundredEightyDay')
}, {
  value: 0,
  label: $t('user.unlimited')
}]
// 最近消费时间
const isNearConTime = ref(false)
const nearConTimeOpts = ref([])
const nearConTimeValue = ref(1)
// 消费次数
const isConNum = ref(false)
const conNumOpts = ref([])
const conNumValue = ref(1)
const conNumBegInp = ref('')
const conNumEndInp = ref('')
// 消费金额
const isConAmount = ref(false)
const conAmountOpts = ref([])
const conAmountValue = ref(1)
const conAmountBegInp = ref('')
const conAmountEndInp = ref('')
// 订单均价
const isOrderAverPrice = ref(false)
const orderAverPriceOpts = ref([])
const orderAverPriceValue = ref(1)
const orderAverPriceBegInp = ref('')
const orderAverPriceEndInp = ref('')
// 充值次数
const isRecharNum = ref(false)
const recharNumOpts = ref([])
const recharNumValue = ref(1)
const recharNumBegInp = ref('')
const recharNumEndInp = ref('')
let userTagId = null
let recharAmountBegInp = null
let recharAmountEndInp = null
let isRecharAmount = null
let recharAmountVlaue = null

watch(() => beCustTimeValue.value, () => {
  changeTimeRan(0)
})
watch(() => followTimeValue.value, () => {
  changeTimeRan(1)
})
watch(() => beMemTimeValue.value, () => {
  changeTimeRan(2)
})
onMounted(() => {
  initPullSelOpts()
  changeTimeRan(0)
  changeTimeRan(1)
  changeTimeRan(2)
})

// 弹出弹框
const init = (_userTagId, tagType) => {
  dialogVisible.value = true
  userTagId = _userTagId
  if (tagType === 0) {
    isDisaCondiLabel.value = true
    labelType.value = '0'
    if (_userTagId !== '') {
      title.value = $t('user.editLabel')
      userTagId = _userTagId
      getEditLabelDetail()
    }
  } else if (tagType === 1) {
    isDisaCustLabel.value = true
    labelType.value = '1'
    if (_userTagId !== '') {
      title.value = $t('user.editLabel')
      userTagId = _userTagId
      getEditLabelDetail()
    }
  }
}
defineExpose({ init })
/**
 * 输入框纯空格处理
 */
const onHandleInputSpaces = (value) => {
  if (!value) return ''
  if (!value.trim()) {
    return value.trim()
  } else {
    return value
  }
}
/**
 * 输入框的数据改变时，对值进行校验
 */
const onCheckNumber = (type) => {
  // 消费次数
  if (type === 1) {
    let BegNum = Math.round(parseInt(conNumBegInp.value))
    if (!BegNum) {
      BegNum = 0
    } else if (BegNum < 0) {
      BegNum = 0
    } else if (BegNum > 100000) {
      BegNum = 100000
    }
    conNumBegInp.value = BegNum
  }
  if (type === 2) {
    let endNum = Math.round(parseInt(conNumEndInp.value))
    if (!endNum) {
      endNum = 0
    } else if (endNum < 0) {
      endNum = 0
    } else if (endNum > 100000) {
      endNum = 100000
    }
    conNumEndInp.value = endNum
  }
  // 消费金额
  if (type === 3) {
    let conBegAmountNum = Math.round(parseFloat(conAmountBegInp.value).toFixed(2))
    if (!conBegAmountNum) {
      conBegAmountNum = 0
    } else if (conBegAmountNum < 0) {
      conBegAmountNum = 0
    } else if (conBegAmountNum >= 100000000) {
      conBegAmountNum = 100000000
    }
    conAmountBegInp.value = conBegAmountNum
  }
  if (type === 4) {
    let conEndAmountNum = Math.round(parseFloat(conAmountEndInp.value).toFixed(2))
    if (!conEndAmountNum) {
      conEndAmountNum = 0
    } else if (conEndAmountNum < 0) {
      conEndAmountNum = 0
    } else if (conEndAmountNum >= 100000000) {
      conEndAmountNum = 100000000
    }
    conAmountEndInp.value = conEndAmountNum
  }
  // 订单均价
  if (type === 5) {
    let ordAverPriceBegNum = Math.round(parseFloat(orderAverPriceBegInp.value).toFixed(2))
    if (!ordAverPriceBegNum) {
      ordAverPriceBegNum = 0
    } else if (ordAverPriceBegNum < 0) {
      ordAverPriceBegNum = 0
    } else if (ordAverPriceBegNum >= 100000000) {
      ordAverPriceBegNum = 100000000
    }
    orderAverPriceBegInp.value = ordAverPriceBegNum
  }
  if (type === 6) {
    let ordAverPriceEndNum = Math.round(parseFloat(orderAverPriceEndInp.value).toFixed(2))
    if (!ordAverPriceEndNum) {
      ordAverPriceEndNum = 0
    } else if (ordAverPriceEndNum < 0) {
      ordAverPriceEndNum = 0
    } else if (ordAverPriceEndNum >= 100000000) {
      ordAverPriceEndNum = 100000000
    }
    orderAverPriceEndInp.value = ordAverPriceEndNum
  }
  // 充值金额
  if (type === 7) {
    let reAmountBegNum = Math.round(parseFloat(recharAmountBegInp).toFixed(2))
    if (!reAmountBegNum) {
      reAmountBegNum = 0
    } else if (reAmountBegNum < 0) {
      reAmountBegNum = 0
    } else if (reAmountBegNum >= 100000000) {
      reAmountBegNum = 100000000
    }
    recharAmountBegInp = reAmountBegNum
  }
  if (type === 8) {
    let reAmountEndNum = Math.round(parseFloat(recharAmountEndInp).toFixed(2))
    if (!reAmountEndNum) {
      reAmountEndNum = 0
    } else if (reAmountEndNum < 0) {
      reAmountEndNum = 0
    } else if (reAmountEndNum >= 100000000) {
      reAmountEndNum = 100000000
    }
    recharAmountEndInp = reAmountEndNum
  }
  // 充值次数
  if (type === 9) {
    let recharBegNum = Math.round(parseInt(recharNumBegInp.value))
    if (!recharBegNum) {
      recharBegNum = 0
    } else if (recharBegNum < 0) {
      recharBegNum = 0
    } else if (recharBegNum > 100000) {
      recharBegNum = 100000
    }
    recharNumBegInp.value = recharBegNum
  }
  if (type === 10) {
    let recharEndNum = Math.round(parseInt(recharNumEndInp.value))
    if (!recharEndNum) {
      recharEndNum = 0
    } else if (recharEndNum < 0) {
      recharEndNum = 0
    } else if (recharEndNum > 100000) {
      recharEndNum = 100000
    }
    recharNumEndInp.value = recharEndNum
  }
}
const initPullSelOpts = () => {
  // 基础条件
  beCustTimeOpts.value = basicConOpts
  beMemTimeOpts.value = basicConOpts
  // 交易条件
  nearConTimeOpts.value = dealConditionOpts
  conNumOpts.value = dealConditionOpts
  conAmountOpts.value = dealConditionOpts
  orderAverPriceOpts.value = dealConditionOpts
  recharNumOpts.value = dealConditionOpts
}
// 获取单个标签的详情
const getEditLabelDetail = () => {
  http({
    url: http.adornUrl('/user/userTag/info/' + userTagId),
    method: 'GET',
    data: http.adornData({})
  }).then(({ data }) => {
    if (labelType.value === '0') {
      labelName.value = data.tagName
    } else if (labelType.value === '1') {
      labelName.value = data.tagName
      // 成为客户时间
      if (data.registerMinTime !== null && data.registerMaxTime !== null) {
        const _beCustTimeRange = []
        isBeCustTimeChecked.value = true
        beCustTimeValue.value = 6
        _beCustTimeRange.push(data.registerMinTime)
        _beCustTimeRange.push(data.registerMaxTime)
        beCustTimeRange.value = _beCustTimeRange
      }
      // 关注时间
      if (data.subscribeMinTime !== null && data.subscribeMaxTime !== null) {
        const _followTimeRange = []
        isfollowTimeChecked = true
        followTimeValue.value = 6
        _followTimeRange.push(data.subscribeMinTime)
        _followTimeRange.push(data.subscribeMaxTime)
        followTimeRange = _followTimeRange
      }
      // 成为会员时间
      if (data.toBeMemberMinTime !== null && data.toBeMemberMaxTime !== null) {
        const _beMemTimeRange = []
        isBeMemTimeChecked.value = true
        beMemTimeValue.value = 6
        _beMemTimeRange.push(data.toBeMemberMinTime)
        _beMemTimeRange.push(data.toBeMemberMaxTime)
        beMemTimeRange.value = _beMemTimeRange
      }
      // 最近消费时间
      if (data.recentPurchaseTime !== null) {
        isNearConTime.value = true
        nearConTimeValue.value = data.recentPurchaseTime
      }
      // 消费次数
      if (data.purchaseNumTime !== null && data.purchaseNumMinNum !== null && data.purchaseNumMaxNum !== null) {
        isConNum.value = true
        conNumValue.value = data.purchaseNumTime
        conNumBegInp.value = data.purchaseNumMinNum
        conNumEndInp.value = data.purchaseNumMaxNum
      }
      // 消费金额
      if (data.purchaseAmountTime !== null && data.purchaseAmountMinAmount !== null && data.purchaseAmountMaxAmount !== null) {
        isConAmount.value = true
        conAmountValue.value = data.purchaseAmountTime
        conAmountBegInp.value = data.purchaseAmountMinAmount
        conAmountEndInp.value = data.purchaseAmountMaxAmount
      }
      // 订单均价
      if (data.orderAveragePriceTime !== null && data.orderAveragePriceMinAmount !== null && data.orderAveragePriceMaxAmount !== null) {
        isOrderAverPrice.value = true
        orderAverPriceValue.value = data.orderAveragePriceTime
        orderAverPriceBegInp.value = data.orderAveragePriceMinAmount
        orderAverPriceEndInp.value = data.orderAveragePriceMaxAmount
      }
      // 充值金额
      if (data.rechargeAmountTime !== null && data.rechargeAmountMinAmount !== null && data.rechargeAmountMaxAmount !== null) {
        isRecharAmount = true
        recharAmountVlaue = data.rechargeAmountTime
        recharAmountBegInp = data.rechargeAmountMinAmount
        recharAmountEndInp = data.rechargeAmountMaxAmount
      }
      // 充值次数
      if (data.rechargeNumTime !== null && data.rechargeNumMinNum !== null && data.rechargeNumMaxNum !== null) {
        isRecharNum.value = true
        recharNumValue.value = data.rechargeNumTime
        recharNumBegInp.value = data.rechargeNumMinNum
        recharNumEndInp.value = data.rechargeNumMaxNum
      }
    }
  })
}
// 修改为自定义范围
const onChagCustTimeRange = (val) => {
  // val：0-成为客户时间 1-关注时间 2-成为会员时间
  if (val === 0) {
    beCustTimeValue.value = 6
  } else if (val === 1) {
    followTimeValue.value = 6
  } else if (val === 2) {
    beMemTimeValue.value = 6
  }
}
// 改变时间范围
const changeTimeRan = (value) => {
  // value: 0-成为客户时间 1-关注时间 2-成为会员时间
  let seleItem = ''
  const timeRange = []
  const time = new Date()
  let beginTime = ''
  let endTime = ''
  if (value === 0) {
    seleItem = beCustTimeValue.value
  } else if (value === 1) {
    seleItem = followTimeValue.value
  } else if (value === 2) {
    seleItem = beMemTimeValue.value
  }

  if (seleItem === 0) {
    beginTime = moment(time).format('LL')
    endTime = beginTime
  } else if (seleItem === 1) {
    beginTime = moment(time).add(-1, 'days').format('L')
    endTime = beginTime
  } else if (seleItem === 2) {
    beginTime = moment(time).add(-7, 'days').format('L')
    endTime = moment(time).add(-1, 'days').format('L')
  } else if (seleItem === 3) {
    beginTime = moment(time).add(-30, 'days').format('L')
    endTime = moment(time).add(-1, 'days').format('L')
  } else if (seleItem === 4) {
    // 本月的开始结束时间
    beginTime = moment(time).startOf('month').format('L')
    endTime = moment(time).endOf('month').format('L')
  } else if (seleItem === 5) {
    // 上个月的开始结束时间
    beginTime = moment(time).add(-1, 'month').startOf('month').format('L')
    endTime = moment(time).add(-1, 'month').endOf('month').format('L')
  }
  if (seleItem !== 6) {
    timeRange.push(beginTime)
    timeRange.push(endTime)
    if (value === 0) {
      beCustTimeRange.value = timeRange
    } else if (value === 1) {
      followTimeRange = timeRange
    } else if (value === 2) {
      beMemTimeRange.value = timeRange
    }
  }
}
// 增加/编辑标签
const onPreserveLabel =
// eslint-disable-next-line max-lines-per-function
() => {
  // 判断是否为整数/零的正则
  const posIntReg = /^([0]|[1-9][0-9]*)$/
  const tagType = labelType.value
  const tagName = labelName.value
  let addOrEditMeth = ''
  if (!tagName || !tagName.trim()) {
    return ElMessage.warning($t('user.labelName') + $t('webInfoConfig.canNotBeEmpty'))
  }
  // 新增条件标签
  if (tagType === '0') {
    // 判断是新增/编辑
    if (userTagId !== '') {
      addOrEditMeth = 'PUT'
    } else {
      addOrEditMeth = 'POST'
    }
    const params = {
      userTagId,
      tagType,
      tagName
    }
    http({
      url: http.adornUrl('/user/userTag'),
      method: addOrEditMeth,
      data: http.adornData(params)
    }).then(() => {
      ElMessage.success($t('publics.operation'))
      dialogVisible.value = false
      emit('refreshLabelList', true)
    })
  } else if (tagType === '1') {
    // 清空条件的参数标记
    let clearRegisterTime = false
    let clearSubscribeTime = false
    let clearToBeMemberTime = false
    let clearRecentPurchaseTime = false
    let clearPurchaseNum = false
    let clearPurchaseAmount = false
    let clearOrderAveragePrice = false
    let clearRechargeAmount = false
    let clearRechargeNum = false
    // 判断是新增/编辑
    if (userTagId !== '') {
      addOrEditMeth = 'PUT'
    } else {
      addOrEditMeth = 'POST'
    }
    // 判断是否选中某个条件，选中就就不能为空
    // 成为客户时间
    let registerMinTime = ''
    let registerMaxTime = ''
    if (isBeCustTimeChecked.value === true && beCustTimeRange.value !== null && beCustTimeRange.value[0] !== '' && beCustTimeRange.value[1] !== '') {
      registerMinTime = moment(beCustTimeRange.value[0]).startOf('days').format('LL')
      registerMaxTime = moment(beCustTimeRange.value[1]).endOf('days').format('LL')
    }
    if (isBeCustTimeChecked.value === false) {
      clearRegisterTime = true
    }
    // 关注时间
    let subscribeMinTime = ''
    let subscribeMaxTime = ''
    if (isfollowTimeChecked === true && followTimeRange !== null && followTimeRange[0] !== '' && followTimeRange[1] !== '') {
      subscribeMinTime = moment(followTimeRange[0]).startOf('days').format('LL')
      subscribeMaxTime = moment(followTimeRange[1]).endOf('days').format('LL')
    }
    if (isfollowTimeChecked === false) {
      clearSubscribeTime = true
    }
    // 成为会员时间
    let toBeMemberMinTime = ''
    let toBeMemberMaxTime = ''
    if (isBeMemTimeChecked.value === true && beMemTimeRange.value !== null && beMemTimeRange.value[0] !== '' && beMemTimeRange.value[1] !== '') {
      toBeMemberMinTime = moment(beMemTimeRange.value[0]).startOf('days').format('LL')
      toBeMemberMaxTime = moment(beMemTimeRange.value[1]).endOf('days').format('LL')
    }
    if (isBeMemTimeChecked.value === false) {
      clearToBeMemberTime = true
    }
    // 最近消费时间
    let recentPurchaseTime = ''
    if (isNearConTime.value === true) {
      recentPurchaseTime = nearConTimeValue.value
    } else {
      clearRecentPurchaseTime = true
    }
    // 消费次数
    let purchaseNumTime = ''
    let purchaseNumMinNum = ''
    let purchaseNumMaxNum = ''
    if (isConNum.value === true) {
      purchaseNumTime = conNumValue.value
      if (posIntReg.test(conNumBegInp.value) && posIntReg.test(conNumEndInp.value)) {
        purchaseNumMinNum = conNumBegInp.value
        purchaseNumMaxNum = conNumEndInp.value
      }
    } else {
      clearPurchaseNum = true
    }
    // 消费金额
    let purchaseAmountTime = ''
    let purchaseAmountMinAmount = ''
    let purchaseAmountMaxAmount = ''
    if (isConAmount.value === true) {
      purchaseAmountTime = conAmountValue.value
      if (posIntReg.test(conAmountBegInp.value) && posIntReg.test(conAmountEndInp.value)) {
        purchaseAmountMinAmount = conAmountBegInp.value
        purchaseAmountMaxAmount = conAmountEndInp.value
      }
    } else {
      clearPurchaseAmount = true
    }
    // 订单均价
    let orderAveragePriceTime = ''
    let orderAveragePriceMinAmount = ''
    let orderAveragePriceMaxAmount = ''
    if (isOrderAverPrice.value === true) {
      orderAveragePriceTime = orderAverPriceValue.value
      if (posIntReg.test(orderAverPriceBegInp.value) && posIntReg.test(orderAverPriceEndInp.value)) {
        orderAveragePriceMinAmount = orderAverPriceBegInp.value
        orderAveragePriceMaxAmount = orderAverPriceEndInp.value
      }
    } else {
      clearOrderAveragePrice = true
    }
    // 充值金额
    let rechargeAmountTime = ''
    let rechargeAmountMinAmount = ''
    let rechargeAmountMaxAmount = ''
    if (isRecharAmount === true) {
      rechargeAmountTime = recharAmountVlaue
      if (posIntReg.test(recharAmountBegInp) && posIntReg.test(recharAmountEndInp)) {
        rechargeAmountMinAmount = recharAmountBegInp
        rechargeAmountMaxAmount = recharAmountEndInp
      }
    } else {
      clearRechargeAmount = true
    }
    // 充值次数
    let rechargeNumTime = ''
    let rechargeNumMinNum = ''
    let rechargeNumMaxNum = ''
    if (isRecharNum.value === true) {
      rechargeNumTime = recharNumValue.value
      if (posIntReg.test(recharNumBegInp.value) && posIntReg.test(recharNumEndInp.value)) {
        rechargeNumMinNum = recharNumBegInp.value
        rechargeNumMaxNum = recharNumEndInp.value
      }
    } else {
      clearRechargeNum = true
    }
    if (tagName.trim() === '') {
      ElMessage.warning($t('user.labelNameNullTips'))
    } else if (isBeCustTimeChecked.value === false && isBeMemTimeChecked.value === false &&
                isConNum.value === false && isConAmount.value === false && isOrderAverPrice.value === false &&
                isRecharNum.value === false && isNearConTime.value === false) {
      ElMessage.warning($t('user.requireOne'))
    } else if (isBeCustTimeChecked.value === true && (beCustTimeRange.value === null || beCustTimeRange.value[0] === '' || beCustTimeRange.value[1] === '')) {
      ElMessage.warning($t('user.becomeCustomerTimeNullTips'))
    } else if (isfollowTimeChecked === true && (followTimeRange === null || followTimeRange[0] === '' || followTimeRange[1] === '')) {
      ElMessage.warning($t('user.followTimeNullTips'))
    } else if (isBeMemTimeChecked.value === true && (beMemTimeRange.value === null || beMemTimeRange.value[0] === '' || beMemTimeRange.value[1] === '')) {
      ElMessage.warning($t('user.becomeMemberTimeNullTips'))
    } else if (isConNum.value === true && (purchaseNumMinNum === '' || purchaseNumMaxNum === '')) {
      ElMessage.warning($t('user.consuNumNullTips'))
    } else if (isConAmount.value === true && (purchaseAmountMinAmount === '' || purchaseAmountMaxAmount === '')) {
      ElMessage.warning($t('user.consuAmountNullTips'))
    } else if (isOrderAverPrice.value === true && (orderAveragePriceMinAmount === '' || orderAveragePriceMaxAmount === '')) {
      ElMessage.warning($t('user.averPriNullTips'))
    } else if (isRecharAmount === true && (rechargeAmountMinAmount === '' || rechargeAmountMaxAmount === '')) {
      ElMessage.warning($t('user.rechargeAmountNullTips'))
    } else if (isRecharNum.value === true && (rechargeNumMinNum === '' || rechargeNumMaxNum === '')) {
      ElMessage.warning($t('user.rechargeNumberNullTips'))
    } else {
      const params = {
        userTagId,
        tagType,
        tagName,
        registerMinTime,
        registerMaxTime,
        subscribeMinTime,
        subscribeMaxTime,
        toBeMemberMinTime,
        toBeMemberMaxTime,
        recentPurchaseTime,
        purchaseNumTime,
        purchaseNumMinNum,
        purchaseNumMaxNum,
        purchaseAmountTime,
        purchaseAmountMinAmount,
        purchaseAmountMaxAmount,
        orderAveragePriceTime,
        orderAveragePriceMinAmount,
        orderAveragePriceMaxAmount,
        rechargeAmountTime,
        rechargeAmountMinAmount,
        rechargeAmountMaxAmount,
        rechargeNumTime,
        rechargeNumMinNum,
        rechargeNumMaxNum,
        clearRegisterTime,
        clearSubscribeTime,
        clearToBeMemberTime,
        clearRecentPurchaseTime,
        clearPurchaseNum,
        clearPurchaseAmount,
        clearOrderAveragePrice,
        clearRechargeAmount,
        clearRechargeNum
      }
      http({
        url: http.adornUrl('/user/userTag'),
        method: addOrEditMeth,
        data: http.adornData(params)
      }).then(() => {
        // eslint-disable-next-line no-const-assign
        ElMessage.success($t('publics.operation'))
        dialogVisible.value = false
        emit('refreshLabelList', true)
      })
    }
  }
}
const onCloseClr = () => {
  emit('refreshLabelList', false)
}

</script>

<style lang="scss" scoped>
.component-user-label-edit{
  .edit-label-content{
    font-size: 12px;
    overflow: auto;
    /* 行内通用 */
    .currency-line{
      display: flex;
      align-items: center;
      margin-bottom: 20px;
    }
    .currency-line .title{
      min-width: 130px;
      font-size: 12px;
      font-weight: normal;
      text-align: right;
      padding: 0 15px 0 0;
      margin: 0;
    }
    .currency-line .must-term{
      color: #F56C6C;
      margin-right: 4px;
    }
    .currency-line :deep(.el-checkbox){
      width: 180px;
      margin-right: 10px;
    }
    .currency-line .module-content{
      padding: 0 15px;
    }
    .currency-line .module-content .option-line{
      display: flex;
      align-items: center;
      margin-bottom: 12px;
    }
    .currency-line .module-content .option-line .unit-wrapper{
      display: flex;
      align-items: center;
    }
    .currency-line .module-content .option-line .first-iterm{
      margin-left: 25px;
    }
    .currency-line .module-content .option-line .interval-line{
      padding: 0 10px;
    }
    .currency-line .module-content .option-line .unit{
      display: inline-block;
      width: 50px;
      height: 40px;
      line-height: 40px;
      background: #efefef;
      border: 1px solid #dcdfe6;
      border-left: none;
      text-align: center;
      border-top-right-radius: 4px;
      border-bottom-right-radius: 4px;
    }
    .currency-line .module-content :deep(.el-select){
      max-width: 185px;
      height: 38px;
      margin-right: 15px;
    }
    .currency-line .module-content .cur-inp{
      width: 100%;
    }
    .currency-line .module-content .cur-inp :deep(.el-input__wrapper){
      border-top-right-radius: 0;
      border-bottom-right-radius: 0;
    }
    /* /行内通用 */
    .label-condition-title-line{
      display: flex;
      align-items: center;
      font-size: 14px;
      font-weight: 700;
      margin-left: 10px;
      margin-bottom: 10px;
    }
    .label-condition-title-line .tips{
      font-size: 12px;
      font-weight: normal;
      color: #595961;
      padding-left: 20px;
    }
  }
}
</style>
